import { isLogin, toLogin, getUserInfo, clearCacheAll } from '../../utils/auth'
import http from '../../utils/http'
import { showModel, showNotify, showToast, showDialog } from '../../utils/index'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        isLogin: false,
        userInfo: null,

        goodsId: null,
        // 商品
        goods: {},

        // 轮播图
        imgUrls: [],

        active: 0,
        tabs: [
            // { id: 1, name: '商品详情' },
            // { id: 2, name: '商品参数' }
        ],

        // 商品购买规格选择
        showSpec: false,
        popupType: null,
        checkedGoodsSku: null
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
        const { goodsId } = options
        this.setData({goodsId})
	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
        // 用户登录信息
        this.setData({ userInfo: getUserInfo() });
        this.setData({ isLogin: isLogin() })
        
        this.getGoodsDetail()
	},


    /**
     * 选择商品规格
     * @param {*} type 
     * @returns 
     */
     showSpecFun(e) {
        if (!isLogin()) return toLogin()
        
        const { type } = e.currentTarget.dataset
        this.setData({ showSpec: true, popupType: type })
    },

    /**
     * 默认选中的规格
     * @param {*} e 
     */
    checkedGoodsSku(e) {
        this.setData({ checkedGoodsSku: e.detail })
    },

    /**
     * 加入购物车
     * @param {*} e 
     */
    async onAddCart(e) {
        let checkSku = e.detail
        let { userInfo } = this.data
        
        let params = {
            memberId: userInfo.id,
            skuId: checkSku.id,
            num: checkSku.goodsNum,
        }
        let res = await http.get('/api/mall/cart/addCartItem', params)
        if(res.status) {
            showToast({title: '已加入购物车', icon: 'success'})
        } else {
            showToast({title: '加入购物车失败'})
        }

        this.setData({ showSpec: false })
    },

    /**
     * 立即购买
     * @param {*} e 
     */
    async onBuy(e) {
        let checkSku = e.detail

        let data = {
            goods: [{skuId: checkSku.id, count: checkSku.goodsNum}]
        }

        wx.navigateTo({ url: '/pages/order_confirm/order_confirm?data=' + JSON.stringify(data) });

        this.setData({ showSpec: false })
    },

    /**
     * 商品详情
     */
    async getGoodsDetail() {
        let res = await http.get('/api/mall/pmsspu/getGoodsInfo', {id: this.data.goodsId})
        
        if(res.status) {
            let goods = res.data

            let urls = []
            goods.imgs.forEach((item, index) => {
                urls.push(item.imgUrl)
            })
            this.setData({ imgUrls: urls })

            console.log(goods);
            this.setData({ goods })
        }

        // let res = await http.get('http://likeshopv2.yixiangonline.com/api/goods/getGoodsDetail?id=10')
        // this.setData({ goods: res.data })

        // let imgs = await http.get('http://likeshopv2.yixiangonline.com/api/goods/getGoodsDetail?id=10')
        // let urls = []
        // imgs.data.goods_image.forEach((item, index) => {
        //     urls.push(item.uri)
        // })
        // this.setData({ imgUrls: urls })
    },

    onTabChange(e) {
        this.setData({ active: e.detail.index })
    },

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {

	}
})